var app = angular.module('newsListApp', []);
app.controller('newsListController', function($scope, $http) {

	$scope.titleClass = [ {
		"li" : "w33 rline",
		"span" : "tabfouse"
	}, {
		"li" : "w33",
		"span" : ""
	}, {
		"li" : "w33",
		"span" : ""
	} ];
	
	$scope.currentCategory = 0;
	$scope.newsListCache = [[], []];
	$scope.newsList = [];

	$scope.titleClick = function(category) {
		$scope.newsList = $scope.newsListCache[category];
		$http({
	        method: 'POST',
	        url: '../query.do',
	        data: { "queryID": "news.S001", "condition": [category] },  
	        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },  
           	transformRequest: function(obj) {
            	var str = [];
            	for (var s in obj) {
                	str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));  
            	}
            	return str.join("&");  
           	}
	    }).then(
	    	function successCallback(response) {
	    		$scope.newsList = response.data.dataList;
	    		$scope.newsListCache[category] = $scope.newsList;
	        }, function errorCallback(response) {
	        	console.log(response);
	    });
		if (category == 0) {
			$scope.titleClass = [ {
				"li" : "w33 rline",
				"span" : "tabfouse"
			}, {
				"li" : "w33",
				"span" : ""
			}, {
				"li" : "w33",
				"span" : ""
			} ];
		} else if (category == 1) {
			$scope.titleClass = [ {
				"li" : "w33",
				"span" : ""
			}, {
				"li" : "w33 rline",
				"span" : "tabfouse"
			}, {
				"li" : "w33",
				"span" : ""
			} ];
		} else if (category == 2) {
			$scope.titleClass = [ {
				"li" : "w33",
				"span" : ""
			}, {
				"li" : "w33",
				"span" : ""
			}, {
				"li" : "w33 rline",
				"span" : "tabfouse"
			} ];
		} 

		$scope.currentCategory = category;
	};
	
	$scope.goDetail = function(link) {
		$("form").attr("action", link);
		$("form").submit();
	};
	
	$scope.titleClick($scope.currentCategory);
});